/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.adama.service;

import fr.adama.dto.AdherentDTO;
import fr.adama.entite.Adherent;
import fr.adama.repository.AdherentReposiroty;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author mini-john
 */
@Service
@Transactional
public class AdherentService extends CrudService {

    private static final Logger logger = LoggerFactory.getLogger(AdherentService.class);
    @Autowired
    AdherentReposiroty adherentRepository;

    public <T> List<T> findByNomPrenom(Class entity, Class entityDTO, String nomPrenom) {

        List list = crudRepository.findByCriteria(entity, Restrictions.like("nomPrenom", nomPrenom));
        List result = new ArrayList<>();
        for (Object objet : list) {
            Object dozerResult = mapperDozer.map(objet, entityDTO);
            result.add(dozerResult);
        }
        return result;

    }

    public List<AdherentDTO> searchByEntity(AdherentDTO adherent) {
        List<AdherentDTO> list = new ArrayList<>();

        List result = adherentRepository.searchByField(adherent);
        for (Object objet : result) {
            AdherentDTO dozerResult = mapperDozer.map(objet, adherent.getClass());
            list.add(dozerResult);
        }

        return list;
    }

    public AdherentDTO findByNomPrenom(String nomPrenom) {
        Adherent list = adherentRepository.getByNomPrenom(nomPrenom);
        if (list == null) {
            return null;
        }
        AdherentDTO dozerResult = mapperDozer.map(list, AdherentDTO.class);

        return dozerResult;

    }

}
